package test.yzx.pproject.act1;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.transitionseverywhere.ChangeBounds;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionManager;

import java.util.ArrayList;

import test.yzx.pproject.R;

public class ListViewItemAnimTestActivity extends AppCompatActivity{

    private ListView lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_item_anim_te);

        lv = (ListView) findViewById(R.id.lv);
        final ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(i+"");
        }

        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            private int itemHeight = 80*3;
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(convertView==null){
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.l_a_item, parent, false);
                }

                ViewGroup.LayoutParams lp = convertView.getLayoutParams();
                if(lp.height != itemHeight){
                    lp.height = itemHeight;
                    convertView.setLayoutParams(lp);
                }

                final View cv = convertView;
                convertView.findViewById(R.id.view).setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        ChangeBounds cb = new ChangeBounds();
                        cb.addListener(new Transition.TransitionListener() {
                            public void onTransitionStart(Transition transition) {
                            }
                            public void onTransitionEnd(Transition transition) {
                                notifyDataSetChanged();
                            }
                            public void onTransitionCancel(Transition transition) {
                            }
                            public void onTransitionPause(Transition transition) {
                            }
                            public void onTransitionResume(Transition transition) {
                            }
                        });

                        TransitionManager.beginDelayedTransition((ViewGroup) cv.getParent(), cb);
                        cv.getLayoutParams().height = 1;
                        cv.requestLayout();
                    }
                });

                return convertView;
            }
        });

    }




}
